import React, { Component } from 'react';
import { getQueryVariable, removeParam } from '../../../util/public'
import fetch from '../../../util/fetch'
import Alert from '../alert'
import './index.scss'
class Header extends Component {
    constructor(props) {
        super(props);
        this.state = { 
            isOpen: false,
        }
    }
    // 跳转个人信息
    gotoPersonalMgr() {
        let sysInfo = JSON.parse(sessionStorage.getItem("sysInfo") || "{}")
        let token = sessionStorage.getItem('token')
        let baseUrl = sessionStorage.getItem('baseUrl')
        let url = "/UserMgr/PersonalMgr/Default.aspx"
        if(sysInfo.iscVersion >= 4){
          url = "html/personalMgr/index.html"
        }
        window.open(baseUrl + url + '?lg_tk=' + token, '_blank');
    }
    // 弹出退出登录弹窗
    changeIsOpen() {
        this.setState({
            isOpen: true
        })
    }
    // 退出登录
    Logout(data) {
        if (data === 'true') {
            let sysInfo = JSON.parse(sessionStorage.getItem("sysInfo") || "{}")
            let token = getQueryVariable("lg_tk") || sessionStorage.getItem("token")
            let baseUrl = sessionStorage.getItem('baseUrl')
            let url = '/api/commons/user/logout'
            let method = "get"
            let obj = {
              token: token,
              sysId: sessionStorage.getItem("sysId"),
              appid: sessionStorage.getItem("sysId"),
              UserID: sessionStorage.getItem("userId"),
              DeviceType: 2
            }
            if(sysInfo.iscVersion >= 4){
              url = baseUrl + "/BaseApi/Login/Logout"
              method = "post"
            }
            fetch[method]({
                url,
                params: obj,
                body: obj
            }).then(res => res.json()).then(result => {
                if (result.code == 200 || result.StatusCode == 200) {
                    let str = removeParam(window.location.href, "lg_tk")
                    window.location = baseUrl + '/login.html?lg_sysid=R11&lg_preurl=' + str
                    sessionStorage.setItem('token', null)
                }
            })
        }
        this.setState({
            isOpen: false
        })
    }
    render() {
        let {
            photoPath,
            userName,
            identityImg,
            identityName,
            isPreset,
        } = JSON.parse(sessionStorage.getItem("userInfo"))
        return (
            <div className="notice-header">
                <div className="notice-header-top">
                    <div className="box-middle">
                        <span className='login_logo'></span>
                        <span>教育信息化指挥中心</span>
                        <div className="notice-header-right">
                            <div className="notice-header-notice" onClick={() => {document.querySelector("#Assistant_infoCenter").click()}}>
                              <i id="Assistant_infoCenter"></i> 
                              消息
                            </div>
                            <div className="split"></div>
                            <div className='notice-header-sculpture' style={{ 'backgroundImage': `url(${photoPath})` }} onClick={() => { this.gotoPersonalMgr() }}></div>
                            <div className='notice-header-name' onClick={() => { this.gotoPersonalMgr() }} >{userName}</div>
                            {
                              isPreset?
                              <img src={identityImg} />:
                              <div className="notice-header-identityImg" title={identityName} style={{backgroundImage: `url(${identityImg})`}}>{identityName}</div>
                            }
                            
                            <i onClick={() => { this.changeIsOpen() }}></i>
                        </div>
                        <div >
                            <span className="notice-header-logn  noticeHeader"  ></span>
                            <span className="notice-header-title" style={{left: (this.props.headerStyle === 2? '88px': '')}}>局校通知公告</span>
                            <span className="notice-header-title1" style={{left: (this.props.headerStyle === 2? '88px': '')}}>{"Announcements"}</span>
                            {/* <span id="circle-title" style={{
                                display: (this.props.type == "circle"? "block": "none")
                            }}>{this.props.circleText}</span> */}
                        </div>
                    </div>
                    <ul className='frame-triangle-wrapper'>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                    </ul>
                    <Alert message='您确定要退出登录么?' isOpen={this.state.isOpen} chooseFn={(data) => { this.Logout(data) }}></Alert>
                    <div>
                        <div> <span></span> <span></span> </div>
                    </div>
                </div>
            </div>
        );
    }
}
 
export default Header;